package dev.alm.javaproject.handler;

import dev.alm.javaproject.common.CommonResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public CommonResponse handleException(Exception e) {
        log.info("enter global exception handler");
        String message = e.getMessage();
        log.info("message is: " + message);
        CommonResponse cr = new CommonResponse();
        cr.fail();
        cr.setMessage(message);
        if (message.equals("token is invalid")) {
            cr.setCode("401");
        }
        return cr;
    }
}
